<template>
	<uni-nav-bar left-icon="back" left-text="返回" right-text="兑现记录" title="兑现" :border="false" @clickLeft="back" @clickRight="more"></uni-nav-bar>
	<view class="content">
		<view class="xuaz">
			<view class="lable-name">兑现方式</view>
			<view class="lable-select">
				<uni-data-select v-model="form.wallet_type" :localdata="walletData" @change="qiehuan()">
					
				</uni-data-select>
			</view>
		</view>
		
		<view class="xuaz">
			<view class="lable-name">打款方式</view>
			<view class="lable-select">
				<uni-data-select v-model="form.type" :localdata="withdrawType" @change="qiehuanType()">
					
				</uni-data-select>
			</view>
		</view>
		
		<view class="xuaz">
			<view class="lable-name" style="width: 20%;">兑换金额</view>
			<view class="lable-select" style="width: 80%;">
				<input type="digit" v-model="form.amount" style="font-size: 14px;" placeholder="请输入兑换金额"/>
			</view>
		</view>
		<view class="xuaz">
			<view class="lable-name" style="width: 60%;">手续费：{{fee}}%</view>
		</view>
		<view class="xuaz">
			<view class="lable-name" style="width: 20%;">交易密码</view>
			<view class="lable-select" style="width: 80%;">
				<input type="safe-password" v-model="form.pay_pass" style="font-size: 14px;" placeholder="交易密码"/>
			</view>
		</view>
	</view>
	
	<view class="content" style="padding: 1rem;text-align: center;">
		<button style="width: 90%;background: #dd5154;color: white;height: 2.5rem; line-height: 2.5rem;" @click="doWithdraw()">确认提交</button>
		<text style="color: #999999;font-size: .7rem;margin-top: 1rem;">50.00$起提 单笔限额5000.00$,且是50.00的倍数
		</text>
		<text @click="jumpPage()" style="padding: 1rem;text-align: center;width: 100%;color: blue;">【完善收款信息】</text>
	</view>
	
</template>

<script>
	export default {
		data() {
			return {
				wallet:null,
				withdraw:null,
				walletData:[
					{
						value:1,
						text:'佣金钱包(余额：0.00)'
					},
					{
						value:4,
						text:'复利钱包(余额：0.00)'
					},
					{
						value:3,
						text:'利息钱包(余额：0.00)'
					}
				],
				withdrawType:[
					{
						value:1,
						text:'银联'
					},
					{
						value:2,
						text:'钱包'
					}
				],
				user_token:null,
				form:{
					wallet_type:null,
					type:null,
					amount:null,
					pay_pass:null
				},
				fee:2
			};
		},
		onLoad() {
			this.getUserInfo();
			this.getConfig();
		},
		methods: {
			getUserInfo(){
				let that = this
				if(that.userInfo == null){
					uni.getStorage({
						key: 'user_token',
						success: function (res) {
							that.user_token = res.data
							that.checkUser()
						},
						fail: function(res){
							uni.showToast({
								title:'请先登录',
								icon:'none',
								success() {
									uni.redirectTo({
										url:'/pages/login/login',
									})
								}
							})
							
						}
					});
				}
			},
			checkUser(){
				let that = this
				uni.request({
					url:getApp().globalData.baseUrl + '/api/user/getUserInfo',
					method:'POST',
					header: {token:that.user_token},
					success(res) {
						if(res.data.code == 401){
							uni.removeStorage({
								key:'user_token',
								success() {
									uni.showToast({
										icon:'none',
										title:'登录已失效,请重新登录',
										success() {
											setTimeout(function(){
												uni.redirectTo({
													url:'/pages/login/login'
												})
											})
										}
									})
								}
							})
						}
						that.userInfo = res.data.data
						that.walletData.forEach(function(value){
							if(value.value == 1){
								value.text = '佣金钱包(余额：' + that.userInfo.money + ')'
							}
							if(value.value == 4){
								value.text = '复利钱包(余额：' + that.userInfo.score + ')'
							}
							if(value.value == 3){
								value.text = '利息钱包(余额：' + that.userInfo.lixi + ')'
							}
							
						})
					},
					fail() {
						uni.showToast({
							title:'网络错误',
							icon:'none',
						})
					}
				})
			},
			uploadImg(data){
				let that = this
				uni.uploadFile({
					url: getApp().globalData.baseUrl + '/api/index/upload',
					filePath: data.tempFilePaths[0],
					success(res) {
						that.form.img = res.data
						that.form.img_url = [{
							name:'ceshi.jpg',
							extname:'jpg',
							url:getApp().globalData.baseUrl + res.data
						}]
					}
				})
			},
			getConfig(){
				let that = this
				uni.request({
					url: getApp().globalData.baseUrl + '/api/index/getConfig',
					method:'POST',
					success(res) {
						console.log(res.data.data.fee)
						that.fee = res.data.data.fee
					}
				})
			},
			doWithdraw(){
				let that = this
				console.log(that.form.wallet_type)
				if(that.form.wallet_type == 1){
					if(that.form.amount > that.userInfo.money){
						uni.showToast({
							title:'余额不足'
						})
						return false;
					}
					
				}
				if(that.form.amount <= 0){
					uni.showToast({
						title:'必须大于0',
						icon:'none'
					})
					return false;
				}
				if(that.form.wallet_type == 3){
					if(that.form.amount > that.userInfo.lixi){
						uni.showToast({
							title:'余额不足',
							icon:'none'
						})
						return false;
					}
					
				}
				if(that.form.wallet_type == 4){
					if(that.form.amount > that.userInfo.score){
						uni.showToast({
							title:'余额不足',
							icon:'none'
						})
						return false;
					}
				}
				if(that.form.amount % 1 > 0){
					uni.showToast({
						title:'必须是1的倍数',
						icon:'none'
					})
					return false;
				}
				uni.request({
					url:getApp().globalData.baseUrl + '/api/withdraw/doWithdraw',
					method:'POST',
					header: {token:that.user_token},
					data: that.form,
					success(res) {
						if(res.data.code == 1){
							uni.showToast({
								title:res.data.msg,
								icon:'none',
								success() {
									setTimeout(function(){
										uni.navigateTo({
											url:"/pages/withdrawList/withdrawList"
										})
									},1000)
								}
							})
						}else{
							uni.showToast({
								title:res.data.msg,
								icon:'none',
							})
						}
					},
					fail() {
						uni.showToast({
							title:'网络错误',
							icon:'none',
						})
					}
				})
			},
			back(){
				uni.navigateBack()
			},
			more(){
				uni.navigateTo({
					url:"/pages/withdrawList/withdrawList"
				})
			},
			qiehuan(data){
				console.log(data)
			},
			qiehuanType(data){
				
			},
			jumpPage(){
				uni.navigateTo({
					url:"/pages/myWalletInfo/myWalletInfo"
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #f4f4f4;
	}
	.xuaz{
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 0 0.6rem;
		padding: 0.6rem;
		border-bottom: 1px solid #f6f6f6 !important;
	}
	.lable-name{
		width: 30%;
		font-weight: bold;
		font-size: 14px;
	}
	.lable-select{
		width: 60%;
		display: flex;
	}
	.content{
		background: #fff;
		display: flex;
		flex-direction: column;
		margin-top: .7rem;
	}
	
	.lable-select >input{
		width: 100%;
	}
</style>
